<?php
/**
 * Created by PhpStorm.
 * User: 17732
 * Date: 2018/6/20
 * Time: 11:21
 */

namespace app\admin\controller;


use think\Controller;
use think\Request;

class Uploadimg extends BaseController
{
    /**
     * 图片上传
     */
    public function uploadImg(Request $request)
    {
//        $url = 'http://library.cc/uploads/';       // url地址
        $url = 'http://qfts.com/uploads/';       // url地址
        $file = $this->request->file('file');
//        dump($file);die;
        if(!empty($file)){
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif'])->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'uploads');
            $error = $file->getError();
            //验证文件后缀后大小
            if(!empty($error)){
                dump($error);exit;
            }
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                $photo = $info->getFilename();

            }else{
                // 上传失败获取错误信息
                $file->getError();
            }
        }else{
            $photo = '';
        }
        if($photo !== ''){
//            $photo = 'http://library.cc/uploads/'.$photo;
            $photo = $url.$photo;
            return ['code'=>1,'msg'=>'成功','photo'=>$photo];
        }else{
            return ['code'=>404,'msg'=>'失败'];
        }
    }
}